<?php
/**
 * UBaseController : U, means user, for login-needed pages
 * 充当拦截器, 过滤未登录用户至登录页面 & 无权限用户至首页
 *
 * Created by PhpStorm.
 * User: silov
 * Date: 16/1/5
 * Time: 10:10
 */
namespace Common\Controller;

use Sys\Model\AdminMenuModel;
use Think\Controller;
use Home\Model\AdminUserModel;

class UBaseController extends Controller
{
    /**
     * @var 当前用户信息
     */
    public $user;
    /**
     * @var HTTP参数集合体
     */
    public $params;
    /**
     * 用户信息初始化
     */
    public function _initialize()
    {
        $uid = session("uid");
        if (!empty($uid)) {
            $adminUserModel = new AdminUserModel();
            $user = $adminUserModel->getAdminUserById($uid);

            if ( $user ) {
                $this->user = $user;
            }
        }

        if (empty($this->user)) {
            //未登录,跳转至登录页面
            $from = $_SERVER['REQUEST_URI'];
            $url = $from ? '/login/signin?from=' . urlencode($from) : '/login/signin';
            $this->error('请先登录再进行浏览', $url , 1);
        }

        $this->assign('user', $this->user);

        $menuModel = new AdminMenuModel();
        $menuList = $menuModel->getForLeftTab();
        $this->assign('navMenu', $menuList);
    }

    /**
     * 设置页面view title
     * @param $title
     */
    public function _title($title)
    {
        $this->assign("_title", $title);
    }

    /**
     * 快速填充表单,仅限get数据
     */
    public function quickBuildForm()
    {
        $this->assign("_params", $_GET);
    }
}